<?php

use LiteView\Kernel\Route;


// 上传发货表
Route::any('/tao/up/deliver_tb', function (\LiteView\Kernel\Visitor $visitor) {
    ini_set('memory_limit', '1000m');
    set_time_limit(300);
    list($error, $validated, $data) = \LiteView\Excel\Excel::read($_FILES['file']['tmp_name'], function ($row, $line) {
        if (1 == $line && '出库方式' != trim($row['I'] ?? '')) {
            \App\Common\General::interrupt(json_encode(['errCode' => 1, 'msg' => '表格格式错误', 'data' => null]), 200);
        }
        if ($line > 1) {
            $sql_data['ka'] = trim($row['B']); // 卡
            $sql_data['mima'] = date('Y-m-d H:i:s', strtotime($row['C'])); // 密码
            $sql_data['order_sn'] = trim($row['D']); // 订单编号
            $sql_data['ck_time'] = date('Y-m-d H:i:s', strtotime($row['F'])); // 出库时间
            $sql_data['rk_time'] = date('Y-m-d H:i:s', strtotime($row['G'])); //入库时间
            $sql_data['ck_uname'] = $row['H'];//出库人
            $sql_data['ck_way'] = $row['I'];//出库方式
            $sql_data['type'] = trim($row['J'] ?? '');//类型
            return ['data' => $sql_data];
        }
    });

    $map = \LiteView\SQL\Crud::db('tao_order')->select('tao_order_deliver', '1', 'ka_sn')->prep()->all();
    $map = array_column($map, null, 'ka_sn');

    $batch = [];
    foreach ($validated as $row) {
        $row['ka_sn'] = md5($row['ka']);
        if (empty($map[$row['ka_sn']])) {
            $batch[] = $row;
        }
    }

    if ($batch) {
        \LiteView\SQL\Crud::db('tao_order')->insertAll('tao_order_deliver', $batch);
    }

    return ['errCode' => 0, 'msg' => '导入成功', 'data' => null];
});

// 上传订单表
Route::any('/tao/up/order_tb', function () {
    ini_set('memory_limit', '1000m');
    set_time_limit(300);
    list($error, $validated, $data) = \LiteView\Excel\Excel::read($_FILES['file']['tmp_name'], function ($row, $line) {
        if (1 == $line && '宝贝总数量' != trim($row['V'] ?? '')) {
            \App\Common\General::interrupt(json_encode(['errCode' => 1, 'msg' => '表格格式错误', 'data' => null]), 200);
        }
        if ($line > 1) {
            $sql_data['order_sn'] = trim($row['A']); // 订单编号
            $sql_data['pay_amount'] = $row['H']; // 实付金额
            $sql_data['goods_cnt'] = $row['V']; // 宝贝数量
            return ['data' => $sql_data];
        }
    });

    $map = \LiteView\SQL\Crud::db('tao_order')->select('tao_orders', '1', 'order_sn')->prep()->all();
    $map = array_column($map, null, 'order_sn');

    $batch = [];
    foreach ($validated as $row) {
        if (empty($map[$row['order_sn']])) {
            $batch[] = $row;
        }
    }

    if ($batch) {
        \LiteView\SQL\Crud::db('tao_order')->insertAll('tao_orders', $batch);
    }

    return ['errCode' => 0, 'msg' => '导入成功', 'data' => null];
});

Route::any('/tao/data', function (\LiteView\Kernel\Visitor $visitor) {
    # 比如7月17号有100个卡号   目的是要知道这100个卡号卖了多少钱
    # 卡订单号里找不到 把 卡号和类型(比如100原版)列出来
    # 100个卡号可能只有90个订单   但是用了98张卡    按订单对下个数对不  不对把订单号给列出来
    # 90个订单的总额多少

    $condition = '1';
    $prep = [];
    if ($val = $visitor->get('type')) {
        $condition .= ' AND m.type = ?';
        $prep[] = trim($val);
    }
    if ($val = $visitor->get('order_sn')) {
        $condition .= ' AND m.order_sn LIKE ?';
        $prep[] = "%$val%";
    }
    if ($val = $visitor->get('has_order')) {
        if (1 == $val) {
            $condition .= ' AND o.id is not null';
        } else {
            $condition .= ' AND o.id is null';
        }
    }
    if ($val = $visitor->get('day_date')) {
        $condition .= ' AND m.mima >= ? AND m.mima <= ?';
        $prep[] = "$val 00:00:00";
        $prep[] = "$val 23:59:59";
    }

    $join = [
        ['tao_orders o', 'm.order_sn = o.order_sn']
    ];

    $limit = $visitor->get('limit', 10);
    $data = \LiteView\SQL\Crud::db('tao_order')->select('tao_order_deliver m', $condition, 'm.*,o.id as order_id,o.pay_amount,o.goods_cnt', $join)->prep($prep)->paginate($limit);

    // 订单总额
    $order_ids = \LiteView\SQL\Crud::db('tao_order')->select('tao_order_deliver m', $condition . ' AND o.id > 0', 'o.id', $join)->prep($prep)->all();
    $order_ids = array_column($order_ids, 'id');
    $order_ids = \App\Common\General::fixIn($order_ids);
    $sum = '0.00';
    if ($order_ids) {
        $sum = \LiteView\SQL\Crud::db('tao_order')->select('tao_orders', "id in($order_ids)", 'sum(pay_amount)')->prep()->column();
    }


    $tb = [
        ['field' => 'id', 'title' => 'ID'],
        ['field' => 'ka', 'title' => '卡'],
        ['field' => 'mima', 'title' => '密码'],
        ['field' => 'order_sn', 'title' => '订单号'],
        ['field' => 'rk_time', 'title' => '入库时间'],
        ['field' => 'ck_time', 'title' => '出库时间'],
        ['field' => 'ck_uname', 'title' => '出库人'],
        ['field' => 'ck_way', 'title' => '出库方式'],
        ['field' => 'type', 'title' => '类型'],
        ['field' => 'order_id', 'title' => '是否有订单'],
        ['field' => 'goods_cnt', 'title' => '宝贝数量'],
        ['field' => 'pay_amount', 'title' => '实际支付金额'],
    ];

    foreach ($data['list'] as &$row) {
        $row['order_id'] = empty($row['order_id']) ? '否' : '是';
    }

    if ($limit >= 100000) {
        $title = [];
        foreach ($tb as $item) {
            $title[$item['field']] = $item['title'];
        }
        \LiteView\Excel\Excel::export($title, $data['list'])->down();
    }

    $all_type = \LiteView\SQL\Crud::db('tao_order')->select('tao_order_deliver', 'type != ""', 'type')->group('type')->prep()->all();
    $all_type = array_column($all_type, 'type');

    return [
        'errCode' => 0,
        'msg' => 'ok',
        'data' => $data,
        'tb' => $tb,
        'sum' => $sum,
        'all_type' => $all_type,
    ];
});

Route::any('/tao/index', function () {
    lite_view('tao/index.php');
});